package mashuaiproject.mashuaiproject.mapper;

import mashuaiproject.mashuaiproject.model.CommentSon;
import mashuaiproject.mashuaiproject.model.Quesions;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface QuesionMapper {

    //更新文章阅读数
    @Update("update quesions set comment_count=comment_count+1 where id=#{id}")
    int updateCommentount(@Param("id") Integer id);

    @Update("update quesions set view_count=view_count+1 where id=#{id}")
    int UpdateViewCount(@Param("id") Integer id);

    @Insert("insert into quesions (title,description,tag,creator,gmt_create,gmt_modified) " +
            "values(#{title},#{description},#{tag},#{creator},#{gmt_create},#{gmt_modified})")
    void creator_publish(Quesions quesions);

    //更新编辑的文章
    @Update("update quesions set title=#{title},description=#{description},tag=#{tag},gmt_create=#{gmt_create}," +
            "gmt_modified=#{gmt_modified} where id=#{id}")
    int updateQueions(Quesions quesions);

    //查找所有文章用于首页展示
    @Select("select *from quesions limit #{offset},#{size}")
    List<Quesions> selectAllQeusions(@Param("offset") Integer offset, @Param("size") Integer size);

    //查找页面的总条数
    @Select("select count(1) from quesions")
    Integer count();

    @Select("select *from quesions where creator=#{id} limit #{offset},#{size}")
    List<Quesions> selectAllQeusions2(@Param("id")Integer id, @Param("offset")Integer offset, @Param("size")Integer size);

    @Select("select count(1) from quesions where creator=#{id} ")
    Integer count2(@Param("id")Integer id);

    @Select("select *from quesions where id=#{id} ")
    Quesions getByidQuesion(@Param("id") Integer id);


    @Select("select *from quesions where id=#{parent_id} ")
    Quesions selectByPrimarytKey(@Param("parent_id") Integer parent_id);

    @Select("select  tag from quesions where id=#{parent_id} ")
    String selectQusionTags(Integer id);

    @Select("select *from quesions where tag regexp #{tag} and id!=#{id}")
    List<Quesions> selctConnetTagQusion(@Param("tag") String connect_tages, @Param("id") Integer id);

    @Select("select *from commentson where father_id=#{fatherId}")
    List<CommentSon> selectAllCommentSonByIds(@Param("fatherId") Integer fatherId);

    @Select("select *from quesions where  title like concat(concat('%',#{serachQuesions},'%'))")
    List<Quesions> selectByTitle(@Param("serachQuesions") String serachQuesions);

    //最新的问题
    @Select("select *from quesions  ORDER BY gmt_create desc limit #{offset},#{size}")
    List<Quesions> selectNewQuesion(Integer offset, Integer size);

    @Select("select *from quesions  ORDER BY view_count desc limit #{offset},#{size}")
    List<Quesions> selectHotQuesion(Integer offset, Integer size);

    @Select("select *from quesions   where comment_count=0 ORDER BY view_count desc limit #{offset},#{size}")
    List<Quesions> selectNoCommentQuesion(Integer offset, Integer size);

    //最热门的话题
    @Select("select *from quesions group by view_count  desc limit 0,5")
    List<Quesions> selectHotQueisons();

    @Select("select creator from quesions where id=#{queiosnIs}")
    Integer slectCreateor(@Param("queiosnIs") Integer queiosnIs);

    @Update("update quesions set like_count=like_count-1 where id=#{id}")
    int updateSaves(@Param("id") Integer del);
    @Select("select  *from quesions where id=#{queiosnId}")
    Quesions selectQueiosns(@Param("queiosnId") Integer parent_id);

    @Select("select  *from quesions where creator=#{useId}")
    List<Quesions> selectOneQueisons(@Param("useId") Integer userId);
}
